package com.ibeeking.found.content.b.rest.service.impl;

import com.ibeeking.found.common.enums.PublishStatusEnum;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.found.content.b.rest.service.IBroadcastLuckyService;
import com.ibeeking.found.content.service.common.constants.enums.BroadcastLuckTypeEnum;
import com.ibeeking.found.content.service.common.constants.enums.BroadcastRedeemWayEnum;
import com.ibeeking.found.content.service.common.dos.BroadcastLuckyDO;
import com.ibeeking.found.content.service.common.param.BroadcastLuckyParam;
import com.ibeeking.found.content.service.common.query.BroadcastLuckyQuery;
import com.ibeeking.found.content.service.common.vo.BroadcastLuckyVO;
import com.ibeeking.found.content.service.mapper.BroadcastLuckyMapper;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BroadcastLuckyServiceImpl  extends ServiceImpl<BroadcastLuckyMapper, BroadcastLuckyDO> implements IBroadcastLuckyService {

    @Resource
    private BroadcastLuckyMapper broadcastLuckyMapper;

    @Override
    public List<BroadcastLuckyVO> list(BroadcastLuckyQuery broadcastLuckyQuery) {
        LambdaQueryWrapper<BroadcastLuckyDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(broadcastLuckyQuery.getLuckyName()), BroadcastLuckyDO::getLuckyName, broadcastLuckyQuery.getLuckyName());
        List<BroadcastLuckyDO> broadcastLuckyDOs = broadcastLuckyMapper.selectList(queryWrapper);
        List<BroadcastLuckyVO> broadcastLuckyVOS = new ArrayList<BroadcastLuckyVO>();
        if(Collections3.isNotEmpty(broadcastLuckyDOs)){
            broadcastLuckyVOS = broadcastLuckyDOs.stream().map(d->{
                BroadcastLuckyVO broadcastLuckyVO = BeanUtil.convertBean(d, BroadcastLuckyVO.class);
                broadcastLuckyVO.setLuckyTypeName(BroadcastLuckTypeEnum.getNameByValue(d.getLuckyType()));
                broadcastLuckyVO.setRedeemWayDesc(BroadcastRedeemWayEnum.getNameByValue(d.getRedeemWay()));
                broadcastLuckyVO.setPublishStatusDesc(PublishStatusEnum.getName(d.getPublishStatus()));
                return broadcastLuckyVO;
            }).collect(Collectors.toList());
        }
        return broadcastLuckyVOS;
    }

    @Override
    public Boolean add(BroadcastLuckyParam broadcastLuckyParam) {
        checkUnique(broadcastLuckyParam);
        BroadcastLuckyDO broadcastLuckyDO = BeanUtil.convertBean(broadcastLuckyParam, BroadcastLuckyDO.class);
        broadcastLuckyDO.setCreateBy(UserUtils.getUserId());
        broadcastLuckyDO.setCreateTime(LocalDateTime.now());
        broadcastLuckyDO.setModifyTime(LocalDateTime.now());
        return broadcastLuckyMapper.insert(broadcastLuckyDO) > 0;
    }

    @Override
    public Boolean modify(BroadcastLuckyParam broadcastLuckyParam) {
        checkUnique(broadcastLuckyParam);
        BroadcastLuckyDO broadcastLuckyDO = BeanUtil.convertBean(broadcastLuckyParam, BroadcastLuckyDO.class);
        broadcastLuckyDO.setModifyBy(UserUtils.getUserId());
        broadcastLuckyDO.setModifyTime(LocalDateTime.now());
        return broadcastLuckyMapper.updateById(broadcastLuckyDO) > 0;
    }

    @Override
    public BroadcastLuckyVO view(Long id) {
        BroadcastLuckyDO broadcastLuckyDO = broadcastLuckyMapper.selectById(id);
        BroadcastLuckyVO broadcastLuckyVO = JsonUtils.fromJson(broadcastLuckyDO, BroadcastLuckyVO.class);
        return broadcastLuckyVO;
    }

    @Override
    public Boolean del(Long id) {
        return broadcastLuckyMapper.deleteById(id) > 0;
    }
    /***
     * 发布状态
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean changePublishStatus(Long id, Integer status) {
        BroadcastLuckyDO broadcastLuckyDO = broadcastLuckyMapper.selectById(id);
        broadcastLuckyDO.setId(id);
        broadcastLuckyDO.setPublishStatus(status);
        broadcastLuckyDO.setModifyTime(LocalDateTime.now());
//        if (PublishStatusEnum.ENABLE.getValue().equals(status)) {
//            broadcastGoodsDO.setPublishTime(LocalDateTime.now());
//        }
        /**
         * 只有点发布时候，才能把商品信息推送到微信直播平台
         */
        return broadcastLuckyMapper.updateById(broadcastLuckyDO) > 0;
    }
    /**
     * 校验唯一性
     *
     * @param param
     */
    public void checkUnique(BroadcastLuckyParam param) {
        LambdaQueryWrapper<BroadcastLuckyDO> queryWrapper = new LambdaQueryWrapper<BroadcastLuckyDO>()
                .ne(param.getId() != null, BroadcastLuckyDO::getId, param.getId())
                .eq(StringUtils.isNotBlank(param.getLuckyName()), BroadcastLuckyDO::getLuckyName, param.getLuckyName());
        List<BroadcastLuckyDO> dos = broadcastLuckyMapper.selectList(queryWrapper);
        if (Collections3.isNotEmpty(dos)) {
            throw new BusinessException("已经存在相同名字的商品!");
        }
    }
}

